package indi.zhifa.recipe.bailan5.coupon.serive.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.recipe.bailan.framework.auth.entity.dto.TokenAuthNodeDto;
import indi.zhifa.recipe.bailan.framework.auth.entity.dto.TokenObject;
import indi.zhifa.recipe.bailan.framework.auth.util.ITokenUtil;
import indi.zhifa.recipe.bailan.framework.mysql.utils.DbDtoEntityUtil;
import indi.zhifa.recipe.bailan.framework.redis.config.RedisConfig;
import indi.zhifa.recipe.bailan.framework.redis.util.RedisUtil;
import indi.zhifa.recipe.bailan5.auth.client.dao.service.ICompanyDbService;
import indi.zhifa.recipe.bailan5.auth.client.util.AuthConst;
import indi.zhifa.recipe.bailan5.coupon.dao.service.ICouponDbService;
import indi.zhifa.recipe.bailan5.coupon.entity.dto.CouponDto;
import indi.zhifa.recipe.bailan5.coupon.entity.po.CouponEntity;
import indi.zhifa.recipe.bailan5.coupon.serive.ICouponService;
import indi.zhifa.recipe.bailan5.coupon.util.AppConst;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Map;

@RequiredArgsConstructor
@Service
public class CouponServiceImpl implements ICouponService {

    private final ICouponDbService mCouponDbService;
    private final ITokenUtil mTokenUtil;
    private final RedisUtil mRedisUtil;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public CouponEntity create(CouponDto pCouponDto) {
        CouponEntity couponEntity = DbDtoEntityUtil.createFromDto(pCouponDto,CouponEntity.class);
        TokenObject tokenObject = mTokenUtil.getTokenObject();
        Map<String, TokenAuthNodeDto> authNodeDtoMap = tokenObject.getAuthNodeInfo();
        TokenAuthNodeDto tokenAuthNodeDto = authNodeDtoMap.get(AuthConst.DOMAIN_COMPANY);
        couponEntity.setCompanyAuthId(tokenAuthNodeDto.getAuthId());
        mCouponDbService.savePull(couponEntity.getId(),couponEntity);
        mRedisUtil.hset(AppConst.COUPON_STORAGE_KEY,couponEntity.getId().toString(),couponEntity.getStorage().intValue());
        return couponEntity;
    }

    @Override
    public CouponEntity edit(Long pId, CouponDto pCouponDto) {
        CouponEntity orgCouponEntity = mCouponDbService.check(pId);
        CouponEntity couponEntity = DbDtoEntityUtil.editByDto(orgCouponEntity,pCouponDto,CouponEntity.class);
        mCouponDbService.updatePull(pId,couponEntity);
        mRedisUtil.hset(AppConst.COUPON_STORAGE_KEY,couponEntity.getId().toString(),couponEntity.getStorage().intValue());
        return couponEntity;
    }

    @Override
    public Page<CouponEntity> page(Integer pCurrent, Integer pSize, Long pCompanyAuthId, String pName) {
        Page<CouponEntity> pageCfg = new Page<>(pCurrent,pSize);
        LambdaQueryWrapper<CouponEntity> queryWrapper = Wrappers.<CouponEntity>lambdaQuery()
                .gt(CouponEntity::getStorage,0)
                .eq(null != pCompanyAuthId,CouponEntity::getCompanyAuthId,pCompanyAuthId)
                .like(StringUtils.hasText(pName),CouponEntity::getName,pName);
        return mCouponDbService.page(pageCfg,queryWrapper);
    }

}
